package com.itheima.controller;

import com.itheima.pojo.*;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/*
* web 登录管理相关接口*/
@RestController
@CrossOrigin
@RequestMapping("/login")
public class WebLoginManageController {


    @Autowired
    private UserService userService;
    //1.POST /login/login (userVO) 用户登录
    //{"code":"error","data":"用户不存在"} {"code":"error","data":"账号或密码错误"}
    @PostMapping("/login")
    public Result login (@RequestBody UserVO userVO,HttpServletRequest request){
        Result result=null;
        try {
           result= userService.login(userVO,request);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }



    //2.POST /login/register (userVO)
    @PostMapping ("/register")
    public  Result register(@RequestBody UserVO userVO, HttpServletRequest request){

       User user = userService.findUser1ByName(userVO.getUserName());
        if (user!=null){
            return new Result(StatusCode.ERROR,"该用户名已存在");
        }
        try {
            int add = userService.register(userVO,request);
            if (add>0){
                return new Result(StatusCode.ERROR,"注册成功，请查看邮箱激活");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(StatusCode.ERROR,"注册失败");
    }

}
